/**
* Copyright (c) By zengqh.
*
* This program is just for fun or demo, in the hope that it  
* will be useful, you can redistribute it and/or modify freely.
*
* Time: 2013/03/07
* File: PixelShader.cpp
**/

#include "OGLPixelShader.h"
#include "OGL.h"

namespace HY
{
	
OGLPixelShader::~OGLPixelShader()
{
	release();
}

uint32 OGLPixelShader::getHandle() const
{
	return handle_;
}

void OGLPixelShader::release()
{
	if (!handle_)
	{
		glDeleteShader(handle_);
		handle_ = 0;
	}
}

const std::string& OGLPixelShader::getSourceCode() const
{
	return shader_code_;
}

void OGLPixelShader::setSourceCode(const std::string& code)
{
	shader_code_ = code;
}

bool OGLPixelShader::compile()
{
	handle_ = glCreateShader(GL_FRAGMENT_SHADER);

	const char* code = shader_code_.c_str();
	if (!code)
	{
		HY_ASSERT(0);
		return false;
	}

	glShaderSource(handle_, 1, &code, 0);

	glCompileShader(handle_);

	GLint nCompiled = GL_FALSE;
	glGetShaderiv(handle_, GL_COMPILE_STATUS, &nCompiled);
	if (nCompiled == GL_FALSE)
	{
		GLchar  msg[1024] = {0};
		GLsizei msgLen = sizeof(msg)/sizeof(msg[0]);

		glGetShaderInfoLog( handle_, msgLen, &msgLen, msg ); 
		printf( "compile error: %s\n", msg );

		HY_ASSERT(0);

		return false;
	}

	return true;
}

}